import type { AxiosInstance, AxiosResponse } from 'axios'
import axios from 'axios'
// 引入进度条
import nprogress from 'nprogress'
// 引入进度条样式
import 'nprogress/nprogress.css'

function request() {
  const instance: AxiosInstance = axios.create({
      baseURL: '',
      timeout: 3000
  })
  setInterceptors(instance)
  return instance
}
//拦截器
function setInterceptors(instance: AxiosInstance) {
  //请求拦截器
  instance.interceptors.request.use((config) => {
    if(config.url != "api/userChat" && config.url != "api/shareSpace"){
      nprogress.start()
    }
    // const token = localStorage.getItem("jwt")
    // if(token){
    //   config.headers.Authorization = token
    // }
    return config
  })
  //响应拦截器
  instance.interceptors.response.use((res: AxiosResponse) => {
    if(res.config.url != "api/userChat" && res.config.url != "api/shareSpace"){
      nprogress.done()
    }
    return res
  }, err => {
    return err;
  })
}

export default request()